<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

class Scan extends Base
{
	// 菜单列表
	public function index()
	{
		$data = $this->request->param();
		if (isset($data['ajax']) && $data['ajax'] == 1) {
			$page = isset($data['page']) ? $data['page'] : 1;
			$page = intval($page);
			$limit = isset($data['rows']) ? $data['rows'] : 10;
			$limit = intval($limit);
			$start = $limit * ($page - 1);

			if (isset($data['search'])) {
				if ($data['tj'] == 0) {
					$sn = Db::name('tagslogs')->where('sn', 'like', '%' . $data['search'] . '%')->select();
					$sn = array_column($sn, 'id');
					$list = Db::name('checklogs')->where('tagslogs_id', 'in', $sn)->order('id desc')->limit($start, $limit)->select();
					$count = Db::name('checklogs')->where('tagslogs_id', 'in', $sn)->count();
				} else {
					$good_id = Db::name('good')->where('name', 'like', '%' . $data['search'] . '%')->select();
					$good_id = array_column($good_id, 'id');
					$list = Db::name('checklogs')->where('good_id', 'in', $good_id)->order('id desc')->limit($start, $limit)->select();
					$count = Db::name('checklogs')->where('good_id', 'in', $good_id)->order('id desc')->count();
				}
			} else {
				$list = Db::name('checklogs')->order('id desc')->limit($start, $limit)->select();
				$count = Db::name('checklogs')->order('id desc')->count();
			}

			foreach ($list as $key => $value) {
				$list[$key]['good_name'] = Db::name('good')->where('id', $value['good_id'])->find()['name'];
				$list[$key]['addtime'] = $value['addtime'] != 0 ? date('Y-m-d H:i:s', $value['addtime']) : '';
				$list[$key]['addr'] = $value['province'] . $value['city'];
				$card = Db::name('card')->where('mobile_phone', $value['telephone'])->find();
				$list[$key]['user'] = $card['user'];
				$list[$key]['danwei'] = $card['danwei'];
				$list[$key]['email'] = $card['email'];
				$list[$key]['tel'] = $card['phone'];
				switch ($value['type']) {
					case 0:
						$list[$key]['type_name'] = '扫码验证';
						break;
					case 1:
						$list[$key]['type_name'] = '电脑验证';
						break;
					case 2:
						$list[$key]['type_name'] = '微信验证';
						break;
				}
				// 查询序列号
				$tagslogs = Db::name('tagslogs')->where('id', $value['tagslogs_id'])->find();
				$list[$key]['sn'] = $tagslogs['sn'];

			}

			return $this->json_data($count, $list);
		}
		return $this->fetch();
	}

	public function daochu()
	{
		$data = $this->request->param();

		if ($data['stime'] != '') {
			$data['stime'] = $data['stime'] . ' 00:00:00';
			$data['etime'] = $data['etime'] . ' 23:59:59';
			$list = Db::name('checklogs')->where('addtime', '>', strtotime($data['stime']))->where('addtime', '<', strtotime($data['etime']))->select();
		} else {
			$list = Db::name('checklogs')->select();
		}
		$tagslogs_id = array_column($list, 'tagslogs_id');
		$tagslogs = Db::name('tagslogs')->where('id', 'in', $tagslogs_id)->select();
		foreach ($list as $key => $value) {
			foreach ($tagslogs as $k => $v) {
				if ($value['tagslogs_id'] == $v['id']) {
					$list[$key]['sn'] = $v['sn'];
					break;
				}
			}
		}

		$sn = array_column($list, 'sn');
		$card = Db::name('card')->where('tagslogs_sn', 'in', $sn)->select();
		foreach ($list as $key => $value) {
			foreach ($card as $k => $v) {
				if ($value['sn'] == $v['tagslogs_sn']) {
					$list[$key]['danwei'] = $v['danwei'];
					$list[$key]['user'] = $v['user'];
					$list[$key]['phone'] = $v['phone'];
					$list[$key]['mobile_phone'] = $v['mobile_phone'];
					$list[$key]['email'] = $v['email'];
					break;
				}
			}
		}

		$goods_id = array_column($list, 'good_id');
		$good = Db::name('good')->where('id', 'in', $goods_id)->select();
		foreach ($list as $key => $value) {
			foreach ($good as $k => $v) {
				if ($value['good_id'] == $v['id']) {
					$list[$key]['good_name'] = $v['name'];
				}
				//'0扫码，1PC验证，2微信',
				switch ($value['type']) {
					case 0:
						$list[$key]['type_name'] = '扫码';
						break;
					case 1:
						$list[$key]['type_name'] = 'PC验证';
						break;
					case 2:
						$list[$key]['type_name'] = '微信';
						break;
					default:
						# code...
						break;
				}
			}
		}

		// echo '<pre>';
		// var_dump($list);
		// exit;

		$spreadsheet = new Spreadsheet();
		$PHPSheet = $spreadsheet->getActiveSheet();

		$PHPSheet->setTitle("验证数据"); //给当前活动sheet设置名称
		$PHPSheet->setCellValue("A1", "商品名称")
			->setCellValue("B1", "验证地址")
			->setCellValue("C1", "验证方式")
			->setCellValue("D1", "验证时间")
			->setCellValue("E1", "用户手机")
			->setCellValue("F1", "所属单位")
			->setCellValue("G1", "单位电话")
			->setCellValue("H1", "联系人")
			->setCellValue("I1", "用户邮箱");

		$styleArray = [
			'alignment' => [
				'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
			],
		];
		foreach ($list as $key => $val) {
			$spreadsheet->setActiveSheetIndex(0)
				->setCellValue("A" . ($key + 2), $val['good_name'])
				->setCellValue("B" . ($key + 2), $val['province'] . $val['city'])
				->setCellValue("C" . ($key + 2), $val['type_name'])
				->setCellValue("D" . ($key + 2), date('Y-m-d H:i:s', $val['addtime']))
				->setCellValue("E" . ($key + 2), $val['telephone'])
				->setCellValue("F" . ($key + 2), $val['danwei'])
				->setCellValue("G" . ($key + 2), $val['phone'])
				->setCellValue("H" . ($key + 2), $val['user'])
				->setCellValue("I" . ($key + 2), $val['email']);
			$spreadsheet->setActiveSheetIndex(0)->getStyle('A' . ($key + 2))->applyFromArray($styleArray);
		}

		$spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(50);
		$spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(20);
		$spreadsheet->getActiveSheet()->getColumnDimension('C')->setWidth(15);
		$spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(15);
		$spreadsheet->getActiveSheet()->getColumnDimension('E')->setWidth(15);
		$spreadsheet->getActiveSheet()->getColumnDimension('F')->setWidth(15);
		$spreadsheet->getActiveSheet()->getColumnDimension('G')->setWidth(15);
		$spreadsheet->getActiveSheet()->getColumnDimension('H')->setWidth(15);
		$spreadsheet->getActiveSheet()->getColumnDimension('I')->setWidth(15);

		$time = date('Y-m-d', time());
		// Redirect output to a client’s web browser (Xlsx)
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment;filename="维修数据' . $time . '.xlsx"');
		header('Cache-Control: max-age=0');
		// If you're serving to IE 9, then the following may be needed
		header('Cache-Control: max-age=1');
		// If you're serving to IE over SSL, then the following may be needed
		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
		header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
		header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
		header('Pragma: public'); // HTTP/1.0

		$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
		$writer->save('php://output');
	}

	// 激活统计
	public function desc()
	{
		// $starttime = date('Y' . '-01-01 00:00:00');
		// $arr = [];
		// for ($i = 1; $i < 13; $i++) {
		// 	$endtime =  date('Y-m-d H:i:s', strtotime("$starttime +1 month -1 second"));
		// 	array_push($arr, Db::name('checklogs')->where('addtime', '>=', strtotime($starttime))->where('addtime', '<=', strtotime($endtime))->count()); 
		// 	$starttime = date('Y-m-d H:i:s', strtotime("$starttime +1 month"));
		// }
		// $this->assign('data', json_encode($arr));
		// return $this->fetch();


		$info['zl'] = Db::name('checklogs')->count(); //总量

		$min_time = mktime(0,0,0,1,1,date('Y'));//本年初时间戳
		$where['addtime'] = ['>=', $min_time];
		$info['year_num'] =  Db::name('checklogs')->where($where)->count(); //年次数
		$min_time = mktime(0,0,0,date('m'),1,date('Y'));//本月初时间戳
		$where1['addtime'] = ['>=', $min_time];
		$info['mon_num'] =  Db::name('checklogs')->where($where1)->count(); //月次数
		$min_time =  mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));  //本周初时间戳

		$where2['addtime'] = ['>=', $min_time];
		$info['week_num'] =  Db::name('checklogs')->where($where2)->count(); //周次数
		//取当前时间的前12月
		$date_arr = [];

		for ($i = 12; $i > 0; $i--) {
			array_push($date_arr, date("Y-m", strtotime("-{$i} month")));
		}
		array_push($date_arr, date("Y-m"));


		$count_sum = [];

		foreach ($date_arr as $k => $val) {
			$starttime = $val . '-01 00:00:00';
			$endtime =  date('Y-m-d H:i:s', strtotime("$starttime +1 month -1 second"));
			array_push($count_sum, Db::name('checklogs')->where('addtime', '>=', strtotime($starttime))->where('addtime', '<=', strtotime($endtime))->count());
		}

		$sheng = Db::table('checklogs')->field('province')->group('province')->select();
		$sheng = array_to_arr($sheng, 'province');
		$sheng_count = [];
		$s = [];
		foreach ($sheng as $k => $val) {
			$s[$val] = Db::name('checklogs')->where('province', 'eq',  $val)->count();
			array_push($sheng_count, Db::name('checklogs')->where('province', 'eq',  $val)->count());
		}
		//halt($sheng);
		//halt($s);
		$this->assign('s', $s);
		$this->assign('sheng', json_encode($sheng));
		$this->assign('sheng_count', json_encode($sheng_count));
		$this->assign('date_arr', json_encode($date_arr));
		$this->assign('count_sum', json_encode($count_sum));
		$this->assign('info', $info);

		return $this->fetch();
	}
}
